package com.ruoyi.patrol.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.patrol.domain.PtCheckRecord;
import com.ruoyi.patrol.mapper.PtCheckRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtCheckAnswerMapper;
import com.ruoyi.patrol.domain.PtCheckAnswer;
import com.ruoyi.patrol.service.IPtCheckAnswerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检查记录填报Service业务层处理
 *
 * @author Du
 * @date 2023-11-21
 */
@Service
public class PtCheckAnswerServiceImpl implements IPtCheckAnswerService {
    @Autowired
    private PtCheckAnswerMapper ptCheckAnswerMapper;

    @Autowired
    private PtCheckRecordMapper ptCheckRecordMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询检查记录填报
     *
     * @param id 检查记录填报ID
     * @return 检查记录填报
     */
    @Override
    public PtCheckAnswer selectPtCheckAnswerById(Long id) {
        return ptCheckAnswerMapper.selectPtCheckAnswerById(id);
    }

    /**
     * 查询检查记录填报列表
     *
     * @param ptCheckAnswer 检查记录填报
     * @return 检查记录填报
     */
    @Override
    public List<PtCheckAnswer> selectPtCheckAnswerList(PtCheckAnswer ptCheckAnswer) {
        return ptCheckAnswerMapper.selectPtCheckAnswerList(ptCheckAnswer);
    }

    /**
     * 新增检查记录填报
     *
     * @param ptCheckAnswer 检查记录填报
     * @return 结果
     */
    @Override
    public int insertPtCheckAnswer(PtCheckAnswer ptCheckAnswer) {
        ptCheckAnswer.setCreateTime(DateUtils.getNowDate());
        return ptCheckAnswerMapper.insertPtCheckAnswer(ptCheckAnswer);
    }

    /**
     * 修改检查记录填报
     *
     * @param ptCheckAnswer 检查记录填报
     * @return 结果
     */
    @Override
    public int updatePtCheckAnswer(PtCheckAnswer ptCheckAnswer) {
        ptCheckAnswer.setUpdateTime(DateUtils.getNowDate());
        return ptCheckAnswerMapper.updatePtCheckAnswer(ptCheckAnswer);
    }

    /**
     * 修改检查记录填报【批量】
     *
     * @param answerList 检查记录填报
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePtCheckAnswerApp(List<PtCheckAnswer> answerList) {
        int i = 0;
        String nickName = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickName();
        for (PtCheckAnswer ptCheckAnswer : answerList) {
            ptCheckAnswer.setUpdateTime(DateUtils.getNowDate());
            ptCheckAnswer.setUpdateBy(nickName);
            ptCheckAnswerMapper.updatePtCheckAnswer(ptCheckAnswer);
            i++;
        }
        PtCheckRecord ptCheckRecord = new PtCheckRecord();
        ptCheckRecord.setId(answerList.get(0).getCheckRecordId());
        ptCheckRecord.setAnswerDate(new Date());
        ptCheckRecord.setStatus("1");
        ptCheckRecord.setInformant(nickName);
        ptCheckRecordMapper.updatePtCheckRecord(ptCheckRecord);
        return answerList.size() == i ? 1 : -1;
    }

    /**
     * 批量删除检查记录填报
     *
     * @param ids 需要删除的检查记录填报ID
     * @return 结果
     */
    @Override
    public int deletePtCheckAnswerByIds(Long[] ids) {
        return ptCheckAnswerMapper.deletePtCheckAnswerByIds(ids);
    }

    /**
     * 删除检查记录填报信息
     *
     * @param id 检查记录填报ID
     * @return 结果
     */
    @Override
    public int deletePtCheckAnswerById(Long id) {
        return ptCheckAnswerMapper.deletePtCheckAnswerById(id);
    }
}
